import 'module-alias/register';
import chalk from 'chalk';
import dayjs from 'dayjs';

import '@/tools/env';
import App from '@/app';
import syncMysqlModel from './tools/syncMysqlModel';
import { project, host } from '@/config';

// tips 更新mysql数据模型,慎用force,因为它会强制更新模型并导致你的数据丢失
// syncMysqlModel('alter');

App.listen(process.env.SERVER_PORT, () => {
  const server = `${project} running success!`;
  console.log(chalk.bgGray.bold(server));
  console.log(chalk.green('\tRunning success time:'));
  console.log(`\t\t${dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')}`);

  switch (process.env.NODE_ENV) {
    case 'development':
      console.log(chalk.green('\tNetwork:'));
      host.forEach((ipv4: string) => {
        console.log(`\t\thttp://${ipv4}:${process.env.SERVER_PORT}`);
      });
      break;
    case 'production':
      console.log(chalk.green('\tProduction address:'));
      console.log(`\t\t${process.env.PRODUCTION_ADDRESS}`);
      break;
    default:
      console.error('error: Lack of environment differentiation (NODE_ENV)');
  }

  console.log(chalk.bgGray.bold(server.split('').reduce((pre) => `${pre}+`, '')));
});
